package com.etc.service;

import com.etc.common.MyServiceException;
import com.etc.dao.EmpMapper;
import com.etc.pojo.EmpMst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmpService {

    @Autowired
    EmpMapper empMapper;

    public List<EmpMst> getAll() {

        return empMapper.selectAll();
    }

    @Transactional(rollbackFor = MyServiceException.class)
    public void addEmp(EmpMst emp) {

        int count = empMapper.add(emp);
        if (count != 1) {
            throw new MyServiceException("添加失败请联系管理员");
        }
    }

    @Transactional(rollbackFor = MyServiceException.class)
    public void deleteById(int id) {
        int r = empMapper.deleteById(id);
        if (r != 1) {
            throw new MyServiceException("删除失败请联系管理员");
        }
    }


    public List<EmpMst> findByCondition(EmpMst emp) {
        List<EmpMst> elist = empMapper.findByCondition(emp);
        if (elist == null || elist.size() == 0) {
            throw new MyServiceException("有0条记录");
        }
        return elist;
    }

    public EmpMst loginEmp(EmpMst emp) {

        EmpMst empMst = empMapper.selectByUsername(emp);

        if (empMst == null) {
            throw new MyServiceException("用户不存在");
        } else if(!emp.getPassword().equals(empMst.getPassword())) {
            throw new MyServiceException("密码不正确");
        }

        return empMst;


    }
}
